package org.jheuristics.ga.operators.reproductors;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.operators.Probability;
import org.jheuristics.ga.operators.Reproductor;
import org.jheuristics.util.GenEnum;
import org.jheuristics.util.RandomGenerator;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell at ourgrid.org, 2005
 */
public class ListEnumsGensListMutation extends AbstractGensListMutation {

	/**
	 * TODO
	 *
	 * @param probability
	 * @param tryOuts
	 */
	public ListEnumsGensListMutation(Probability probability, int tryOuts) {
		super(probability, tryOuts);
	}

	/**
	 * TODO
	 *
	 * @param decoretedReproductor
	 * @param probability
	 * @param tryOuts
	 */
	public ListEnumsGensListMutation(Reproductor decoretedReproductor, Probability probability, int tryOuts) {
		super(decoretedReproductor, probability, tryOuts);
	}

	/**
	 * TODO
	 *
	 * @param object
	 * @param status
	 * @param config
	 * @return
	 * @see org.jheuristics.ga.operators.reproductors.AbstractGensListMutation#mutate(java.lang.Object, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	protected Object mutate(Object enums, GAStatus status, GAConfig config) {
		Probability probability = getProbability();
		RandomGenerator random = config.getRandomGenerator();

		List newEnums = new LinkedList();
		for (Iterator it = ((List) enums).iterator(); it.hasNext(); ) {
			GenEnum genEnum = (GenEnum) it.next();
			if (probability.check(status, config)) {
				newEnums.add(genEnum.random(random));
			} else {
				newEnums.add(genEnum);
			}
		}
		return newEnums;
	}

}
